package com.beimihu.leetcode;

public class GasCost {
    public int canCompleteCircuit(int[] gas, int[] cost) {

        int n = gas.length;
        int i = 0;

        while (i < n){
            int index = 0;
            int sumGas = 0, sumCost = 0;
            while (index < n){
                int j = (index + i ) % n;
                sumGas = sumGas + gas[j];
                sumCost = sumCost + cost[j];
                if(sumGas < sumCost){
                    break;
                }
                index++;

            }
            if (index == i){
                return index;
            }
            else{
                i = i + index + 1 ;
            }
        }
        return - 1 ;
    }
}
